﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Kooboo.Commerce.AddIn;
using System.ComponentModel.Composition;
using Kooboo.Commerce.Promotions.DiscountByCategory.Models;

namespace Kooboo.Commerce.Promotions.DiscountByCategory
{
    [Export(typeof(IPromotion))]
    public class DiscountByCategoryPromotion : IPromotion
    {
        public string Name
        {
            get { return "DiscountByCategory"; }
        }

        public string HeadLine
        {
            get { return "DiscountByCategory"; }
        }

        public IPromotionViews Views
        {
            get { return new DiscountByCategoryView(); }
        }

        public bool Execute(IPromotionRule rule, Connect.User user, PromotionContext context)
        {
            var settings = Services.Current.Find(rule);
            if (settings != null)
            {
                if (settings.ReduceType == (int)ReduceType.ByFee)
                {
                    return DiscountByFee(settings, context);
                }
                else
                {
                    return DiscountByPercent(settings, context);
                }
            }

            return false;
        }

        public void RemoveSettings(IPromotionRule rule, Connect.User user)
        {
            Services.Current.Remove(rule);
        }

        private bool DiscountByPercent(Promotion_DiscountByCategory rule, PromotionContext context)
        {
            Boolean hasExcute = false;
            decimal totalDiscount = 0;
            if (context.Items != null && context.Items.Count > 0)
            {
                foreach (var item in context.Items)
                {
                    if (item.Product.Categories.Where(c => c.Id == rule.CategoryId).Count() > 0)
                    {
                        hasExcute = true;

                        var discount = item.SubTotal * rule.ReduceValue;
                        item.SubTotal -= discount;
                        totalDiscount += discount;
                    }
                }
            }

            if (hasExcute)
            {
                context.Discount += totalDiscount;
                context.Total -= totalDiscount;
            }

            return hasExcute;
        }

        private bool DiscountByFee(Promotion_DiscountByCategory rule, PromotionContext context)
        {
            Boolean hasExcute = false;
            decimal totalDiscount = 0;
            if (context.Items != null && context.Items.Count > 0)
            {
                foreach (var item in context.Items)
                {
                    if (item.Product.Categories.Where(c => c.Id == rule.CategoryId).Count() > 0)
                    {
                        hasExcute = true;

                        var discount = rule.ReduceValue;

                        item.SubTotal -= discount;
                        totalDiscount += discount;
                    }
                }
            }

            if (hasExcute)
            {
                context.Discount += totalDiscount;
                context.Total -= totalDiscount;
            }

            return hasExcute;
        }
    }
}